//
//  HMOAuthViewController.m
//  weibo
//
//  Created by apple on 2016/11/17.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HMOAuthViewController.h"
#import "HMAccountViewModel.h"
#import "HMwkwebView.h"
@interface HMOAuthViewController ()<UIWebViewDelegate>
@property(nonatomic,weak)UIWebView *webView;

@end

@implementation HMOAuthViewController

#pragma mark
#pragma mark - 程序入口
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addNavigationitem];
    
    [self visitWebView];
}
-(void)addNavigationitem{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStyleDone target:self action:@selector(close)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"直达天宫" style:UIBarButtonItemStylePlain target:self action:@selector(addDirectionEnter)];
//    document.getElementById('userId').value="18858660584"
//    document.getElementById('passwd').value="19910406"
    self.navigationItem.rightBarButtonItem = rightItem;
}
-(void)close{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)addDirectionEnter{
    NSString *str1 = @"document.getElementById('userId').value='18858660584',document.getElementById('passwd').value='w199146'";
    [self.webView stringByEvaluatingJavaScriptFromString:str1];
}
-(void)visitWebView{
    //https://api.weibo.com/oauth2/authorize?client_id=123050457758183&redirect_uri=http://www.example.com/response&response_type=code
    NSString *urlString = @"https://api.weibo.com/oauth2/authorize?client_id=3011133703&redirect_uri=http://www.itheima.com";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    UIWebView *webView = [[UIWebView alloc]init];
    webView.delegate = self;
    [webView loadRequest:request];
    [self.view addSubview:webView];
    _webView = webView;
[webView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.view);
}];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD show];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
}



-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString *str = request.URL.absoluteString;
    if ([str containsString:@"code="]) {
        NSString *query = request.URL.query;
        NSString *fromstr = @"code=";
        NSString *code = [query substringFromIndex:fromstr.length];
    
        [[HMAccountViewModel shared]loadAccesstokenCode:code isSucceedBlock:^(BOOL isSucceed) {
            
            if (!isSucceed) {
                [SVProgressHUD showErrorWithStatus:networkWarn];
                return ;//直接返回，，这个返回的是block的代码块
            }
            [[NSNotificationCenter defaultCenter]postNotificationName:ChooseNotificationController object:@"welcome"];
        }];
        return false;
    }
    return true;
}

@end
